*/
function ParserTest() {
global $options;
-
+
# Only colorize output if stdout is a terminal.
$this->lightcolor = false;
$this->color = !wfIsWindows() && posix_isatty(1);
-
+
if( isset( $options['color'] ) ) {
switch( $options['color'] ) {
case 'no':
break;
}
}
-
+
$this->showDiffs = !isset( $options['quick'] );
-
+
$this->quiet = isset( $options['quiet'] );
if (isset($options['regex'])) {
return $s;
}
}
-
+
/**
* Run a series of tests listed in the given text file.
* Each test consists of a brief description, wikitext input,
if( !$infile ) {
die( "Couldn't open parserTests.txt\n" );
}
-
+
$data = array();
$section = null;
$success = 0;
$result = $this->tidy($result);
}
-
+
$this->teardownGlobals();
-
+
if( $result === $out && $this->wellFormed( $out ) ) {
return $this->showSuccess( $desc );
} else {
return $this->showFailure( $desc, $result, $out );
}
}
-
+
/**
* Set up the global variables for a consistent environment for each test.
* Ideally this should replace the global configuration entirely.
if( !isset( $this->uploadDir ) ) {
$this->uploadDir = $this->setupUploadDir();
}
-
+
$settings = array(
'wgServer' => 'http://localhost',
'wgScript' => '/index.php',
'wgContLanguageCode' => 'en',
'wgDBprefix' => 'parsertest',
'wgDefaultUserOptions' => array(),
-
+
'wgLang' => new LanguageUtf8(),
'wgContLang' => new LanguageUtf8(),
'wgNamespacesWithSubpages' => array( 0 => preg_match('/\\bsubpage\\b/i', $opts)),
$GLOBALS['wgLoadBalancer']->loadMasterPos();
$GLOBALS['wgMessageCache']->initialise( new BagOStuff(), false, 0, $GLOBALS['wgDBname'] );
$this->setupDatabase();
-
+
global $wgUser;
$wgUser = new User();
}
-
+
# List of temporary tables to create, without prefix
# Some of these probably aren't necessary
function listTables() {
'objectcache'
);
}
-
+
/**
* Set up a temporary set of wiki tables to work with for the tests.
* Currently this will only be done once per run, and any changes to
'img_major_mime' => "image",
'img_minor_mime' => "jpeg",
) );
-
+
$setupDB = true;
}
}
-
+
/**
* Create a dummy uploads directory which will contain a couple
* of files in order to pass existence tests.
*/
function setupUploadDir() {
global $IP;
-
+
$dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
mkdir( $dir );
mkdir( $dir . '/3' );
mkdir( $dir . '/3/3a' );
-
+
$img = "$IP/skins/monobook/headbg.jpg";
$h = fopen($img, 'r');
$c = fread($h, filesize($img));
fclose($h);
-
+
$f = fopen( $dir . '/3/3a/Foobar.jpg', 'wb' );
fwrite( $f, $c );
fclose( $f );
return $dir;
}
-
+
/**
* Restore default values and perform any necessary clean-up
* after each test runs.
unset( $this->uploadDir );
}
}
-
+
/**
* Remove the dummy uploads directory
* @access private
@rmdir( "$dir/thumb" );
rmdir( "$dir" );
}
-
+
/**
* "Running test $desc..."
* @access private
function showTesting( $desc ) {
print "Running test $desc... ";
}
-
+
/**
* Print a happy success message.
*
}
return true;
}
-
+
/**
* Print a failure message and provide some explanatory output
* about what went wrong if so configured.
}
return false;
}
-
+
/**
* Run given strings through a diff and return the (colorized) output.
* Requires writable /tmp directory and a 'diff' command in the PATH.
*/
function quickDiff( $input, $output, $inFileTail='expected', $outFileTail='actual' ) {
$prefix = wfTempDir() . "/mwParser-" . mt_rand();
-
+
$infile = "$prefix-$inFileTail";
$this->dumpToFile( $input, $infile );
-
+
$outfile = "$prefix-$outFileTail";
$this->dumpToFile( $output, $outfile );
-
+
$diff = `diff -au $infile $outfile`;
unlink( $infile );
unlink( $outfile );
-
+
return $this->colorDiff( $diff );
}
-
+
/**
* Write the given string to a file, adding a final newline.
*
fwrite( $file, $data . "\n" );
fclose( $file );
}
-
+
/**
* Return ANSI terminal escape code for changing text attribs/color,
* or empty string if color output is disabled.
return $this->color ? "\x1b[{$color}m" : '';
}
}
-
+
/**
* Return ANSI terminal escape code for restoring default text attributes,
* or empty string if color output is disabled.
function termReset() {
return $this->color ? "\x1b[0m" : '';
}
-
+
/**
* Colorize unified diff output if set for ANSI color output.
* Subtractions are colored blue, additions red.
}
return $text;
}
-
+
/**
* Hack up a private DOCTYPE with HTML's standard entity declarations.
* PHP 4 seemed to know these if you gave it an HTML doctype, but
$out .= "]>\n";
return $out;
}
-
+
function wellFormed( $text ) {
$html =
$this->hackDocType() .
'<html>' .
$text .
'</html>';
-
+
$parser = xml_parser_create( "UTF-8" );
-
+
# case folding violates XML standard, turn it off
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
-
+
if( !xml_parse( $parser, $html, true ) ) {
$err = xml_error_string( xml_get_error_code( $parser ) );
$position = xml_get_current_byte_index( $parser );
xml_parser_free( $parser );
return true;
}
-
+
function extractFragment( $text, $position ) {
$start = max( 0, $position - 10 );
$before = $position - $start;
$this->termColor( 0 );
return "$display\n$caret";
}
-
+
}
?>